<?php

namespace app\common\model;

// 引入构建器
use think\model\relation\BelongsTo;

class CourseQuestion extends Base
{
    // 定义时间戳字段名
    protected $a_time = 'a_time';
    protected $u_time = 'u_time';


    public function course(): BelongsTo
    {
        return $this->belongsTo('Course', 'course_id');
    }

    /**
     * 单选Json格式
     */
    public function getType1Map(): array
    {
        return [
            //题目
            'questionAsk' => '?',

            //答案可选项
            'answerOption' => [
                'A' => 'a',
                'B' => 'b',
                'C' => '',
                'D' => '',
                'E' => '',
                'F' => '',
                'G' => '',
                'H' => '',
            ],

            //正确答案
            'correctOption' => 'B',

            //答案解析
            'explanation' => '',
        ];
    }

    /**
     * 多选Json格式
     */
    public function getType2Map(): array
    {
        return [
            //题目
            'questionAsk' => '?',

            //答案可选项
            'answerOption' => [
                'A' => 'a',
                'B' => 'b',
                'C' => '',
                'D' => '',
                'E' => '',
                'F' => '',
                'G' => '',
                'H' => '',
            ],

            //正确答案
            'correctOption' => 'ABC',

            //答案解析
            'explanation' => '',
        ];
    }

    /**
     * 判断Json格式
     */
    public function getType3Map(): array
    {
        return [
            //题目
            'questionAsk' => '?',

            //答案可选项
            'answerOption' => [
                'A' => 'a',
                'B' => 'b',
            ],

            //正确答案
            'correctOption' => '',

            //答案解析
            'explanation' => '',
        ];
    }

    /**
     * 问答Json格式
     */
    public function getType4Map(): array
    {
        return [
            //题目
            'questionAsk' => '?',

            //正确答案
            'correctOption' => '',

            //答案解析
            'explanation' => '',
        ];
    }

    /**
     * 填空Json格式
     */
    public function getType5Map(): array
    {
        return [
            //题目
            'questionAsk' => '?',

            //正确答案
            'correctOption' => '...',

            //答案解析
            'explanation' => '',
        ];
    }

    /**
     * 名词Json格式
     */
    public function getType8Map(): array
    {
        return [
            //题目
            'questionAsk' => '?',

            //正确答案
            'correctOption' => '...',

            //答案解析
            'explanation' => '',
        ];
    }

    /**
     * getQuestionTypeMap
     * @return string[]
     */
    public function getQuestionTypeMap(): array
    {
        return [
            '1' => '单择题',
            '2' => '多选题',
            '3' => '判断题',
            '4' => '问答题',
            '5' => '填空题',
            '6' => '完形填空',
            '7' => '阅读理解',
            '8' => '名词解释',
        ];
    }

    /**
     * getQuestionTypeNameByTypeId
     * @param $typeId
     * @return string
     */
    public function getQuestionTypeNameByTypeId($typeId): string
    {
        $allTypeMap = $this->getQuestionTypeMap();
        return $allTypeMap[$typeId] ?? '-';
    }

    public function getExtendQuestionCount($course_id,$extend_ids)
    {
        $where = [
            ['course_id', '=', $course_id],
            ['extend_id', 'in', $extend_ids],
        ];
        $field = 'extend_id,count(*) as count';
        $list = $this->where($where)->field($field)->group('extend_id')->select();
        return $list ? $list->toArray() : [];
    }

}